import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/models/common/LevelOption.dart';
import 'package:ichinesereader/pages/common/levelStandards.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/forms/levelChoiceChip.dart';
import 'package:ichinesereader/widgets/grids/bookGridWithCheckBox.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/widgets/grids/bookGridWithCheckBoxTeaching.dart';
import '../../controller/cache.dart';
import '../../controller/user.dart';
import '../addons/quickSearchBooks.dart';
import 'package:badges/badges.dart' as myBadges;

class OpenLibraryReadingBody extends StatefulWidget {
  OpenLibraryReadingBody({
    Key? key,
  }) : super(key: key);

  OpenLibraryReadingBodyState createState() =>
      new OpenLibraryReadingBodyState();
}

class OpenLibraryReadingBodyState extends State<OpenLibraryReadingBody> {
  final BooksController _booksController = Get.find();
  final UserController _usersController = Get.find();
  int limit = Constants.pageLimit;
  var box = Hive.box('appconfig');
  bool isStudentOrIndividual = false;

  @override
  void initState() {
    isStudentOrIndividual = box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Individual ||
        box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Student;
    _booksController.setBookSearchDto(new BookSearchDto());
    Future.delayed(Duration(milliseconds: 100), () {
      if (isStudentOrIndividual) {
        _usersController.refreshLevelPercentage();
      }
      //_booksController.selectedLibraryReadingBookItemsClear();
      _booksController.fetchOpenReadingItems(limit, 0);
    });
    super.initState();
  }

  @override
  void dispose() {
    List<LevelOption> levelOptions = [];
    _booksController.setLevelOptions(levelOptions);
    _booksController.openLibraryReadingBookItems.clear();
    _booksController.selectedLibraryReadingBookItemsClear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build OpenReadingBody');
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Row(
          children: [
            SizedBox(
                width: MediaQuery.of(context).size.width > 450
                    ? 450
                    : MediaQuery.of(context).size.width * 0.85,
                child: QuickSearchBooksWidget()),
            //Spacer(),
            Expanded(
              child: myBadges.Badge(
                badgeStyle: myBadges.BadgeStyle(
                  badgeColor: Colors.red.withOpacity(0.8),
                ),
                position: myBadges.BadgePosition.topEnd(top: 0, end: 3),
                badgeContent: GestureDetector(
                  onTap: () {
                    Get.to(() => LevelStandardsPage(isEvaluation: false));
                  },
                  child: Tooltip(
                    message: 'Level Comparison Chart'.tr,
                    child: Icon(
                        const IconData(
                          0xf04e5,
                          fontFamily: 'MaterialIcons',
                        ),
                        color: Colors.white,
                        size: 30),
                  ),
                ),
                child: Container(
                  height: 45.0,
                  child: Text(''),
                ),
              ),
            ),
          ],
        ),
        LevelChioceChipWidget(),
        //SizedBox(height: 5),
        Flexible(
          child: Center(
            child: _booksController.obx(
              (data) => isStudentOrIndividual
                  ? BookGridWithCheckBox(
                      books: _booksController.openLibraryReadingBookItems,
                      readingType: NanhaiBookReadingTypes.library,
                    )
                  : BookGridWithCheckBoxTeaching(
                      books: _booksController.openLibraryReadingBookItems,
                      readingType: NanhaiBookReadingTypes.library,
                    ),
              onEmpty: Text('Books not available'.tr),
              onLoading: Container(),
            ),
          ),
        ),
      ],
    );
  }
}
